﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyModel;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Nest;
using Swashbuckle.AspNetCore.SwaggerUI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using static System.Net.WebRequestMethods;

namespace Magic.Core;

/// <summary>
/// swagger配置
/// </summary>
public static class SwaggerServiceCollectionExtensions
{
    public static IServiceCollection AddSwagger(this IServiceCollection services)
    {
        // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
        services.AddEndpointsApiExplorer();
        services.AddSwaggerGen(options =>
        {
            var scheme = "Bearer";
            options.AddSecurityDefinition(scheme, new OpenApiSecurityScheme()
            {
                Description = "在下框中输入请求头中需要添加Jwt授权Token",
                Name = "JWT Authorization",
                In = ParameterLocation.Header,
                Type = SecuritySchemeType.Http,
                BearerFormat = "JWT",
                Scheme = scheme,
                Reference = new OpenApiReference
                {
                    Id = scheme,
                    Type = ReferenceType.SecurityScheme
                }
            });

            options.AddSecurityRequirement(new OpenApiSecurityRequirement
            {
                {
                    new OpenApiSecurityScheme{
                    Reference = new OpenApiReference {
                    Type = ReferenceType.SecurityScheme,
                    Id = "Bearer"}
                    },new string[] { }
                }
            });

            var dir = new DirectoryInfo(AppContext.BaseDirectory);
            var files = dir.GetFiles("Magic.*.xml");
            foreach (var file in files)
            {
                options.IncludeXmlComments(file.FullName, true);
            }
            options.SwaggerDoc("system", new OpenApiInfo
            {
                Version = "system",//版本
                Title = "Magic系统模块",//标题
                Description = "Magic HTTP API",//描述
                Contact = new OpenApiContact
                {
                    Name = "蛋蛋",
                    Email = "guo812557927.com@qq.com"
                }
            });
            options.SwaggerDoc("student", new OpenApiInfo { Title = "客户管理", Version = "student" });
        });

        return services;
    }

    public static void UseSwaggerAndUI(this WebApplication app)
    {
        if (app.Environment.IsDevelopment())
        {
            app.UseSwagger(c =>
            {

                c.PreSerializeFilters.Add((swagger, httpReq) =>
                {
                    // 默认 Server
                    var servers = new List<OpenApiServer> {
                        new OpenApiServer { Url = $"{httpReq.Scheme}://{httpReq.Host.Value}",Description="Default" }
                    };
                    swagger.Servers = servers;
                });
            });

            app.UseSwaggerUI(c =>
            {
                c.DocumentTitle = "Magic";
                c.SwaggerEndpoint("/swagger/system/swagger.json", "系统模块");
                c.SwaggerEndpoint("/swagger/student/swagger.json", "学生管理");
                c.DocExpansion(DocExpansion.None);
            });
        }
    }
}

